package org.epam.szhuk.greenway.activity;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.util.List;

import org.epam.szhuk.greenway.R;
import org.epam.szhuk.greenway.util.FileProcessor;
import org.epam.szhuk.greenway.util.Preferences;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera.Size;
import android.media.CamcorderProfile;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceClickListener;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class PreferencesActivity extends PreferenceActivity {
	public final static String TAG="PreferencesActivity";
	private ProgressDialog pDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
		
		// About button
		Preference rowAbout = (Preference) findPreference("showAbout");
		final Context context= (Context)this;
		rowAbout.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			public boolean onPreferenceClick(Preference preference) {
				PackageInfo pInfo = null;
				String version = null;
				try {
					pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
					version = pInfo.versionName;
				} catch (NameNotFoundException e) {
					Log.e(TAG,"NameNotFoundException: ", e);
					version = "?";
				}
				
				new AlertDialog.Builder(context)
						.setIcon(R.drawable.ic_launcher)
						.setTitle("About")
						.setMessage("Version" +" "+ version + " HACKATHON\n" + "\n"
										+ "Team Market Pool")
						.setPositiveButton("Ok", null).show();
				return true;
			}
		});
		
		// Delete all speed files button
		Preference rowDeleteSpeedFiles = (Preference) findPreference("deleteRecordedSpeedFiles");
		
		rowDeleteSpeedFiles.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			public boolean onPreferenceClick(Preference preference) {
				SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
				String appRootFolder=pref.getString("appFilesFolder", "simplecamera");
				final String appSpeedFilesFolder=Environment.getExternalStorageDirectory() +  File.separator +appRootFolder;
				AlertDialog.Builder builder=new AlertDialog.Builder(context);
				builder.setTitle(R.string.app_name);
				builder.setMessage(getResources().getString(R.string.delete_all_speed_files_from_folder) +" \""+appSpeedFilesFolder+ "\" ?");
			    builder.setPositiveButton(context.getString(android.R.string.ok),new DialogInterface.OnClickListener(){
			    	public void onClick(DialogInterface dialog,int id){
			    		if(FileProcessor.deleteAllTracks(appSpeedFilesFolder)){
							myToast(getResources().getString(R.string.speed_files_were_successful_deleted_from_fld)+" \""+appSpeedFilesFolder +"\\");
						}
						else {
							Log.e(TAG, "Can't delete all speed files in the app folder");
							myToast("Can't delete all speed files in app folder");
							return;
						}
			    	}
			    });
			                 
			    builder.setNegativeButton(context.getString(android.R.string.cancel),new DialogInterface.OnClickListener(){
					public void onClick(DialogInterface dialog,int id){
						dialog.cancel();
				    }
				});
			    builder.setCancelable(false);
			    builder.create();
				builder.show();
				return true;
			}
		});
		
	
	}
	
	private void myToast(String text){
		Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
	}
}